cmake_minimum_required(VERSION 3.16)

project(certificate_framework_unittest LANGUAGES C CXX)

include(${CMAKE_CURRENT_SOURCE_DIR}/common.cmake)

set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

target_compile_options(EXE_COMPILE_CONFIG INTERFACE
  -Wno-unused-lambda-capture # warning: lambda capture 'this' is not used [-Wunused-lambda-capture]
)

set(CRYPTO_FRAMEWORK_INC
  ${CRYPTO_FRAMEWORK_DIR}/interfaces/inner_api/common
  ${CRYPTO_FRAMEWORK_DIR}/interfaces/inner_api/algorithm_parameter
  ${CRYPTO_FRAMEWORK_DIR}/interfaces/inner_api/crypto_operation
  ${CRYPTO_FRAMEWORK_DIR}/interfaces/inner_api/key
)

set(CERTIFICATE_FRAMEWORK_INC
  ${DEPEND_INC}
  ${CRYPTO_FRAMEWORK_INC}
  ${SOURCE_DIR}/interfaces/inner_api/include
  ${SOURCE_DIR}/interfaces/inner_api/common
  ${SOURCE_DIR}/interfaces/inner_api/certificate
  ${SOURCE_DIR}/interfaces/inner_api/attestation
  ${SOURCE_DIR}/frameworks/common/v1.0/inc
  ${SOURCE_DIR}/frameworks/ability/inc
  ${SOURCE_DIR}/frameworks/adapter/v1.0/inc
  ${SOURCE_DIR}/frameworks/adapter/v2.0/inc
  ${SOURCE_DIR}/frameworks/adapter/attestation/inc
  ${SOURCE_DIR}/frameworks/core/cert/inc
  ${SOURCE_DIR}/frameworks/core/extension/inc
  ${SOURCE_DIR}/frameworks/core/life/inc
  ${SOURCE_DIR}/frameworks/core/param/inc
  ${SOURCE_DIR}/frameworks/core/v1.0/spi
)

# certificate_framework_core
file(GLOB_RECURSE CERTIFICATE_FRAMEWORK_CORE_SRC
  ${SOURCE_DIR}/frameworks/core/*.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/*.c
  ${SOURCE_DIR}/frameworks/adapter/v2.0/*.c
  ${SOURCE_DIR}/frameworks/adapter/attestation/*.c
)

add_library(certificate_framework_core SHARED ${CERTIFICATE_FRAMEWORK_CORE_SRC})
target_include_directories(certificate_framework_core PRIVATE ${CERTIFICATE_FRAMEWORK_INC})
target_link_directories(certificate_framework_core PRIVATE ${DEPEND_LIB})
target_link_libraries(certificate_framework_core
  PRIVATE LIB_COMPILE_CONFIG
  PUBLIC LIB_LINK_CONFIG
)

# cert
file(GLOB_RECURSE CERT_SRC
  ${SOURCE_DIR}/frameworks/common/*.c
  ${SOURCE_DIR}/frameworks/ability/*.c
)

add_library(cert SHARED ${CERT_SRC})
target_include_directories(cert PRIVATE ${CERTIFICATE_FRAMEWORK_INC})
target_link_directories(cert PRIVATE ${DEPEND_LIB})
target_link_libraries(cert
  PRIVATE LIB_COMPILE_CONFIG
  PUBLIC LIB_LINK_CONFIG
)

set(CERTIFICATE_FRAMEWORK_TEST_INC
  ${CERTIFICATE_FRAMEWORK_INC}
  ${SOURCE_DIR}/frameworks/adapter/attestation/src
  ${SOURCE_DIR}/test/unittest/common/include
  ${SOURCE_DIR}/test/unittest/v1.0/include
)

# cf_adapter_test
file(GLOB_RECURSE CF_ADAPTER_TEST_SRC
  ${SOURCE_DIR}/test/unittest/common/src/cf_test_common.cpp
  ${SOURCE_DIR}/test/unittest/cf_adapter_test/*.cpp
)

add_executable(cf_adapter_test ${CF_ADAPTER_TEST_SRC})
add_dependencies(cf_adapter_test certificate_framework_core cert)
target_include_directories(cf_adapter_test PRIVATE ${CERTIFICATE_FRAMEWORK_TEST_INC})
target_link_directories(cf_adapter_test PRIVATE ${DEPEND_LIB})
target_link_libraries(cf_adapter_test
  PRIVATE EXE_COMPILE_CONFIG
  PUBLIC EXE_LINK_CONFIG certificate_framework_core cert
)

# cf_core_test
file(GLOB_RECURSE CF_CORE_TEST_SRC
  ${SOURCE_DIR}/test/unittest/cf_core_test/*.cpp
)

add_executable(cf_core_test ${CF_CORE_TEST_SRC})
add_dependencies(cf_core_test certificate_framework_core cert)
target_include_directories(cf_core_test PRIVATE ${CERTIFICATE_FRAMEWORK_TEST_INC})
target_link_directories(cf_core_test PRIVATE ${DEPEND_LIB})
target_link_libraries(cf_core_test
  PRIVATE EXE_COMPILE_CONFIG
  PUBLIC EXE_LINK_CONFIG certificate_framework_core cert
)

# cf_sdk_test
file(GLOB_RECURSE CF_SDK_TEST_SRC
  ${SOURCE_DIR}/test/unittest/common/src/cf_test_common.cpp
  ${SOURCE_DIR}/test/unittest/common/src/cf_test_sdk_common.cpp
  ${SOURCE_DIR}/test/unittest/cf_sdk_test/*.cpp
)

add_executable(cf_sdk_test ${CF_SDK_TEST_SRC})
add_dependencies(cf_sdk_test certificate_framework_core cert)
target_include_directories(cf_sdk_test PRIVATE ${CERTIFICATE_FRAMEWORK_TEST_INC})
target_link_directories(cf_sdk_test PRIVATE ${DEPEND_LIB})
target_link_libraries(cf_sdk_test
  PRIVATE EXE_COMPILE_CONFIG
  PUBLIC EXE_LINK_CONFIG certificate_framework_core cert
)

# cf_version1_test
set(CF_VERSION1_MOCK_SRC
  ${SOURCE_DIR}/frameworks/common/v1.0/src/cf_blob.c
  ${SOURCE_DIR}/frameworks/common/v1.0/src/cf_object_base.c
  ${SOURCE_DIR}/frameworks/common/v1.0/src/utils.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/certificate_openssl_common.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_cert_chain_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_cert_chain_openssl_ex.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_cert_chain_validator_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_certificate_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_crl_entry_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_crl_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_distinguished_name_openssl.c
  ${SOURCE_DIR}/frameworks/adapter/v1.0/src/x509_cert_cms_generator_openssl.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/cert_chain_validator.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/cert_crl_collection.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/cert_crl_common.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/x509_cert_chain.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/x509_certificate.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/x509_crl.c
  ${SOURCE_DIR}/frameworks/core/v1.0/certificate/x509_distinguished_name.c
)

file(GLOB_RECURSE CF_VERSION1_TEST_SRC
  ${CF_VERSION1_MOCK_SRC}
  ${SOURCE_DIR}/test/unittest/common/src/cf_test_common.cpp
  ${SOURCE_DIR}/test/unittest/common/src/cf_test_sdk_common.cpp
  ${SOURCE_DIR}/test/unittest/v1.0/*.c
  ${SOURCE_DIR}/test/unittest/v1.0/*.cpp
)

add_executable(cf_version1_test ${CF_VERSION1_TEST_SRC})
add_dependencies(cf_version1_test certificate_framework_core cert)
target_include_directories(cf_version1_test PRIVATE ${CERTIFICATE_FRAMEWORK_TEST_INC})
target_link_directories(cf_version1_test PRIVATE ${DEPEND_LIB})
target_link_libraries(cf_version1_test
  PRIVATE EXE_COMPILE_CONFIG
  PUBLIC EXE_LINK_CONFIG crypto_common crypto_openssl_plugin crypto_framework
  certificate_framework_core cert
)

create_wrap_function(function_wrap "wrap_function.txt")
target_link_libraries(cf_version1_test PRIVATE function_wrap)

install(TARGETS certificate_framework_core cert
  LIBRARY DESTINATION lib
)

install(TARGETS cf_adapter_test cf_core_test cf_sdk_test cf_version1_test
  RUNTIME DESTINATION bin
)
